    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views.Dialog;

import com.sun.org.apache.xml.internal.dtm.ref.sax2dtm.SAX2DTM2;
import controllers.entity.ChamThi;
import controllers.process.CanBoProcess;
import controllers.process.ChamThiProcess;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import models.Constant;
import views.JPanel.TTCanBo;


/**
 *
 * @author HoaDuyen
 */
public class DlgThemChamThi extends java.awt.Dialog {

    /**
     * Creates new form DlgThemChamThi
     */
    private CanBoProcess canBoProcess;
    private ChamThiProcess chamThiProcess;
    private int macanbo;
    private ChamThi chamthi;
    private TTCanBo parent;
    public DlgThemChamThi(JFrame parent, boolean modal,int macanbo,ChamThi chamthi ) {
        super(parent, modal);
        this.parent=(TTCanBo)parent;
        this.chamthi=chamthi;
        this.macanbo=macanbo;
        this.setResizable(false);
        canBoProcess=new CanBoProcess();
        chamThiProcess=new ChamThiProcess();
        this.setLocation(Constant.LOCATE_XDLG, Constant.LOCATE_YDLG);
        initComponents();
        ok.setText("Thêm");
        if (chamthi != null) { // Dang o che do sua
            malop.setText(chamthi.getMaLopHoc());
            mamonhoc.setText(chamthi.getMaMonHoc());
            malop.setEnabled(false);
            ngaynop.setDate(chamthi.getNgayNop());
            sobaithi.setText(""+chamthi.getSoBaiThi());
            txtChamThi.setText("Sửa thông tin chấm thi");
            ok.setText("Lưu");
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        txtChamThi = new javax.swing.JLabel();
        ok = new javax.swing.JButton();
        cancel = new javax.swing.JButton();
        jPanel14 = new javax.swing.JPanel();
        jLabel19 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        malop = new javax.swing.JTextField();
        mamonhoc = new javax.swing.JTextField();
        jPanel15 = new javax.swing.JPanel();
        jLabel22 = new javax.swing.JLabel();
        jLabel23 = new javax.swing.JLabel();
        sobaithi = new javax.swing.JTextField();
        ngaynop = new com.toedter.calendar.JDateChooser();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        txtChamThi.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        txtChamThi.setText("Thêm Thông Tin");

        ok.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/btn_add.png"))); // NOI18N
        ok.setText("Thêm");
        ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okActionPerformed(evt);
            }
        });

        cancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/btn_delete.png"))); // NOI18N
        cancel.setText("Hủy");
        cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        jLabel19.setText("Mã Lớp");

        jLabel21.setText("Mã Môn Học");

        javax.swing.GroupLayout jPanel14Layout = new javax.swing.GroupLayout(jPanel14);
        jPanel14.setLayout(jPanel14Layout);
        jPanel14Layout.setHorizontalGroup(
            jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel14Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel14Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel21)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(mamonhoc, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(24, 24, 24))
                    .addGroup(jPanel14Layout.createSequentialGroup()
                        .addComponent(jLabel19)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(malop, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(23, 23, 23))))
        );
        jPanel14Layout.setVerticalGroup(
            jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel14Layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addGroup(jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel19)
                    .addComponent(malop, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(mamonhoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel21))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        jLabel22.setText("Ngày Nộp");

        jLabel23.setText("Số Bài Thi");

        sobaithi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sobaithiActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel15Layout = new javax.swing.GroupLayout(jPanel15);
        jPanel15.setLayout(jPanel15Layout);
        jPanel15Layout.setHorizontalGroup(
            jPanel15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel15Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(jPanel15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel22)
                    .addComponent(jLabel23))
                .addGap(18, 18, 18)
                .addGroup(jPanel15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ngaynop, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)
                    .addComponent(sobaithi)))
        );
        jPanel15Layout.setVerticalGroup(
            jPanel15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel15Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(jPanel15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel22)
                    .addComponent(ngaynop, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel23)
                    .addComponent(sobaithi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(181, 181, 181)
                        .addComponent(ok)
                        .addGap(18, 18, 18)
                        .addComponent(cancel))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel14, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(48, 48, 48)
                        .addComponent(jPanel15, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(167, 167, 167)
                        .addComponent(txtChamThi)))
                .addContainerGap(60, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtChamThi)
                .addGap(26, 26, 26)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel14, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel15, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 57, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ok)
                    .addComponent(cancel))
                .addGap(61, 61, 61))
        );

        cancel.getAccessibleContext().setAccessibleName("cancel");

        add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Closes the dialog
     */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog

    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        dispose();
    }//GEN-LAST:event_cancelActionPerformed

    private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed
        if (malop.getText().length()>29){
            JOptionPane.showMessageDialog(this,"Kích thước không hợp lệ !");
            malop.selectAll();
            malop.requestFocus();
            return;
        }
        if (mamonhoc.getText().length()>9){
            JOptionPane.showMessageDialog(this,"Kích thước không hợp lệ !");
            mamonhoc.selectAll();
            mamonhoc.requestFocus();
            return;
        }
        if (malop.getText().equals("")|| mamonhoc.getText().equals("")||sobaithi.getText().equals("")){
            JOptionPane.showMessageDialog(this, "Không được bỏ trống!");
            return;
        }
        String tmalop = malop.getText();
        String tmamonhoc=mamonhoc.getText();
        Date tngaynop = ngaynop.getDate();
        if (tngaynop == null) {
            JOptionPane.showMessageDialog(this, "Phải chọn ngày!");
            return;
        }
        java.util.Date today = new java.util.Date();
        if ((today.getTime() - ngaynop.getDate().getTime())/31536000 >1*1000){
            JOptionPane.showMessageDialog(this, "Lỗi ! Ngay nop sai");
            return;
        }
         if ((today.getTime() - ngaynop.getDate().getTime())/31536000<-1*1000){
            JOptionPane.showMessageDialog(this, "Lỗi ! Ngay nop sai");
            return;
        }
        Integer tsobaithi;
        try {
                    tsobaithi= Integer.parseInt(sobaithi.getText());
        } catch (Exception e1) {
                    JOptionPane.showMessageDialog(null, "Phải nhập số nguyên !");
                    sobaithi.selectAll();
                    sobaithi.requestFocus();
                    return;
        }
        if (tsobaithi<0){
            JOptionPane.showMessageDialog(null, "Không được <0!");
                    sobaithi.selectAll();
                    sobaithi.requestFocus();
                    return;
        }
        
       
        ChamThi ct=new ChamThi(tmalop,macanbo,tmamonhoc,tsobaithi,tngaynop);
        if (chamthi == null) { // Them moi sach
            if (ChamThiProcess.insertChamThi(ct)) {
                JOptionPane.showMessageDialog(this, "Thêm thành công!");
                parent.loadListChamThi();
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(this, "Lỗi,kiểm tra lại SQL service!");
            }
        } else {
            //sua sach
            if (ChamThiProcess.updateChamThi(ct)) {
                JOptionPane.showMessageDialog(this, "Sửa thành công!");
                parent.loadListChamThi();
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(this, "Lỗi,kiểm tra lại SQL service!");
            }
        }
       
    }//GEN-LAST:event_okActionPerformed

    private void sobaithiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sobaithiActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_sobaithiActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancel;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel14;
    private javax.swing.JPanel jPanel15;
    private javax.swing.JTextField malop;
    private javax.swing.JTextField mamonhoc;
    private com.toedter.calendar.JDateChooser ngaynop;
    private javax.swing.JButton ok;
    private javax.swing.JTextField sobaithi;
    private javax.swing.JLabel txtChamThi;
    // End of variables declaration//GEN-END:variables
}
